﻿using System;
using System.Collections.Generic;
using System.Text;
using MYLSOFT.JsonSimple;
using com.byt.suce.db.mapper;
using com.byt.suce.db.factory;
using com.byt.webcore;
using com.byt.webcore.utils;
using com.byt.gestor.service;
using suce.upload;
using System.Reflection;
using System.IO;

namespace com.byt.gestor.service
{
    abstract class DGSBase
    {

        internal void Procesar(string mensaje, JSONObject jo, Connection gsuce, Connection vuce)
        {
            mensaje = mensaje.ToUpper().Replace("-", "_");

            Type cls = this.GetType();
            MethodInfo m = cls.GetMethod(mensaje, BindingFlags.NonPublic | BindingFlags.Instance);
            if (m != null)
                m.Invoke(this, new object[] { jo, gsuce, vuce });
        }

        protected void N2(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N2.get
            PkgGestorq.N2GetParam getN2 = PkgGestorqFactory.create().n2Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.OrdenAceptarParam paramsVuce = PkgGestorServicioFactory.create().newOrdenAceptarParam();
            paramsVuce.Codformato = getN2.Codformato;
            paramsVuce.Docnumero = getN2.Docnumero;
            paramsVuce.Tasa = Utils.parseDouble(getN2.Tasa);
            PkgGestorServicioFactory.create().ordenAceptar(vuce, paramsVuce);

            //N2.set
            PkgGestorq.N2SetParam setN2 = PkgGestorqFactory.create().newN2SetParam();
            setN2.Idq = getN2.Idq;
            setN2.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n2Set(gsuce, setN2);
        }

        protected void N7(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N7.get
            PkgGestorq.N7GetParam getN7 = PkgGestorqFactory.create().n7Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceIniciarTramiteParam paramsVuce = PkgGestorServicioFactory.create().newSuceIniciarTramiteParam();
            paramsVuce.Docnumero = getN7.Docnumero;
            paramsVuce.Numexpediente = getN7.Numexpediente;
            PkgGestorServicioFactory.create().suceIniciarTramite(vuce, paramsVuce);

            //N7.set
            PkgGestorq.N7SetParam setN7 = PkgGestorqFactory.create().newN7SetParam();
            setN7.Idq = getN7.Idq;
            setN7.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n7Set(gsuce, setN7);
        }

        abstract protected void N8(JSONObject jo, Connection gsuce, Connection vuce);

        abstract protected void N9(JSONObject jo, Connection gsuce, Connection vuce);

        protected void N11(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N11.get
            PkgGestorq.N11GetParam getN11 = PkgGestorqFactory.create().n11Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.OrdenSubsanarParam paramsVuce = PkgGestorServicioFactory.create().newOrdenSubsanarParam();
            paramsVuce.Numdocumento = getN11.Numeorden;
            paramsVuce.Texto = getN11.Texto;
            PkgGestorServicioFactory.create().ordenSubsanar(vuce, paramsVuce);

            //N11.set
            PkgGestorq.N11SetParam setN11 = PkgGestorqFactory.create().newN11SetParam();
            setN11.Idq = getN11.Idq;
            setN11.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n11Set(gsuce, setN11);
        }

        protected void N15(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N15.get
            PkgGestorq.N15GetParam getN15 = PkgGestorqFactory.create().n15Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.MtoRechazarParam paramsVuce = PkgGestorServicioFactory.create().newMtoRechazarParam();
            paramsVuce.Numeroorden = getN15.Numeorden;
            paramsVuce.Numeromto = getN15.Numemto;
            paramsVuce.Texto = getN15.Texto;
            PkgGestorServicioFactory.create().mtoRechazar(vuce, paramsVuce);

            //N15.set
            PkgGestorq.N15SetParam setN15 = PkgGestorqFactory.create().newN15SetParam();
            setN15.Idq = getN15.Idq;
            setN15.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n15Set(gsuce, setN15);
        }

        protected void N17(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N17.get
            PkgGestorq.N17GetParam getN17 = PkgGestorqFactory.create().n17Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceSubsanarParam paramsVuce = PkgGestorServicioFactory.create().newSuceSubsanarParam();
            paramsVuce.Numsuce = getN17.Numesuce;
            paramsVuce.Tasa = getN17.Tasa;
            paramsVuce.Plazo = getN17.Plazo;
            paramsVuce.Texto = getN17.Texto;
            PkgGestorServicioFactory.create().suceSubsanar(vuce, paramsVuce);

            // Construir el ZIP de adjuntos
            string numeorden = getN17.Numeorden;
            int? idsubsanacion = getN17.Idsubsanacion;
            string pathZip = UploadHelper.GetPath(numeorden.Substring(1,10).ToString() + "\\" + numeorden.Substring(0,11).ToString(), null, paramsVuce.Idnotificacion + ".zip");
            Compresion.ComprimirArchivos(pathZip, UploadHelper.ListFilesStringFullName(Path.Combine(numeorden.Substring(1, 10).ToString() + "//" + numeorden, "NS" + idsubsanacion.ToString())));

            //N17.set
            PkgGestorq.N17SetParam setN17 = PkgGestorqFactory.create().newN17SetParam();
            setN17.Idq = getN17.Idq;
            setN17.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n17Set(gsuce, setN17);
        }

        protected void N19(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N19.get
            PkgGestorq.N19GetParam getN19 = PkgGestorqFactory.create().n19Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.MosRechazarParam paramsVuce = PkgGestorServicioFactory.create().newMosRechazarParam();
            paramsVuce.Nummos = getN19.Numemos;
            paramsVuce.Numsuce = getN19.Numesuce;
            paramsVuce.Texto = getN19.Texto;
            PkgGestorServicioFactory.create().mosRechazar(vuce, paramsVuce);

            // Construir el ZIP de adjuntos
            //string numeorden = getN19.Numeorden;// "O2013001654";//getN19.Numeorden;
            string numer = getN19.Numeorden;
            string numeorden = numer.Substring(1,10).ToString() + "\\" + numer;//CB
            int? idsubsanacion = getN19.Version;// 4;//getN19.Idsubsanacion;* es version*
            string pathZip = UploadHelper.GetPath(numer.Substring(1,10).ToString() + "\\" + numer.Substring(0,11).ToString(), null, paramsVuce.Idnotificacion + ".zip");
            Compresion.ComprimirArchivos(pathZip, UploadHelper.ListFilesStringFullName(Path.Combine(numer.Substring(1, 10).ToString() + "\\" + numer.Substring(0, 11).ToString(), "NS" + idsubsanacion.ToString())));

            //N19.set
            PkgGestorq.N19SetParam setN19 = PkgGestorqFactory.create().newN19SetParam();
            setN19.Idq = getN19.Idq;
            setN19.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n19Set(gsuce, setN19);
        }

        protected void N21(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N21.get
            PkgGestorq.N21GetParam getN21 = PkgGestorqFactory.create().n21Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.MosAprobarAmpliacionParam paramsVuce = PkgGestorServicioFactory.create().newMosAprobarAmpliacionParam();
            paramsVuce.Nummos = getN21.Numemos;
            paramsVuce.Numsuce = getN21.Numesuce;
            PkgGestorServicioFactory.create().mosAprobarAmpliacion(vuce, paramsVuce);

            //N21.set
            PkgGestorq.N21SetParam setN21 = PkgGestorqFactory.create().newN21SetParam();
            setN21.Idq = getN21.Idq;
            setN21.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n21Set(gsuce, setN21);
        }

        protected void N22(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N22.get
            PkgGestorq.N22GetParam getN22 = PkgGestorqFactory.create().n22Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceMosAprobarParam paramsVuce = PkgGestorServicioFactory.create().newSuceMosAprobarParam();
            paramsVuce.Nummos = getN22.Nummos;
            paramsVuce.Numsuce = getN22.Numsuce;
            paramsVuce.Tasa = Utils.parseDouble(getN22.Tasa);
            PkgGestorServicioFactory.create().suceMosAprobar(vuce, paramsVuce);

            //N22.set
            PkgGestorq.N22SetParam setN22 = PkgGestorqFactory.create().newN22SetParam();
            setN22.Idq = getN22.Idq;
            setN22.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n22Set(gsuce, setN22);
        }

        protected void N36(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N36.get
            PkgGestorq.N36GetParam getN36 = PkgGestorqFactory.create().n36Get(gsuce, jo);
            JSONObject traza = Utils.getJSONObject(getN36.CurOut);
            getN36.CurOut.Close();

            // Enviar el N36 al integrador
            PkgGestorServicio.TrazaEnviarParam paramsVuce = PkgGestorServicioFactory.create().newTrazaEnviarParam();
            paramsVuce.Idtrazagestor = traza["idtraza"].ToString();
            paramsVuce.Doctipo = (string)traza["doctipo"];
            paramsVuce.Docnum = (string)traza["docnum"];
            paramsVuce.Area = (string)traza["area"];
            paramsVuce.Subarea = (string)traza["subarea"];
            paramsVuce.Responsable = (string)traza["responsable"];
            paramsVuce.Fechaingreso = Utils.parseDate((string)traza["fechaingreso"]);
            paramsVuce.Fechasalida = Utils.parseDate((string)traza["fechasalida"]);
            paramsVuce.Comentario = (string)traza["mensaje"];
            PkgGestorServicioFactory.create().trazaEnviar(vuce, paramsVuce);

            // Construir el XML
            GestorToVuce ebxml = new GestorToVuce();
            ebxml.N36(traza, paramsVuce.Idnotificacion);

            //N36.set
            PkgGestorq.N36SetParam setN36 = PkgGestorqFactory.create().newN36SetParam();
            setN36.Idq = getN36.Idq;
            setN36.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n36Set(gsuce, setN36);
        }

        protected void N39(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N39.get
            PkgGestorq.N39GetParam getN39 = PkgGestorqFactory.create().n39Get(gsuce, jo);

            // Enviar el N39 al integrador
            PkgGestorServicio.SuceCerrarParam paramsVuce = PkgGestorServicioFactory.create().newSuceCerrarParam();
            paramsVuce.Numsuce = getN39.Numesuce;
            paramsVuce.Numdr = getN39.Numedr;
            PkgGestorServicioFactory.create().suceCerrar(vuce, paramsVuce);

            //N39.set
            PkgGestorq.N39SetParam setN39 = PkgGestorqFactory.create().newN39SetParam();
            setN39.Idq = getN39.Idq;
            setN39.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n39Set(gsuce, setN39);
        }

        protected void N48(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N48.get
            PkgGestorq.N48GetParam getN48 = PkgGestorqFactory.create().n48Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceAceptadesistirParam paramsVuce = PkgGestorServicioFactory.create().newSuceAceptadesistirParam();
            paramsVuce.Numsuce = getN48.Numesuce;
            PkgGestorServicioFactory.create().suceAceptadesistir(vuce, paramsVuce);

            //N48.set
            PkgGestorq.N48SetParam setN48 = PkgGestorqFactory.create().newN48SetParam();
            setN48.Idq = getN48.Idq;
            setN48.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n48Set(gsuce, setN48);
        }

        protected void N49(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N49.get
            PkgGestorq.N49GetParam getN49 = PkgGestorqFactory.create().n49Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceRechazadesistirParam paramsVuce = PkgGestorServicioFactory.create().newSuceRechazadesistirParam();
            paramsVuce.Numsuce = getN49.Numesuce;
            PkgGestorServicioFactory.create().suceRechazadesistir(vuce, paramsVuce);

            //N49.set
            PkgGestorq.N49SetParam setN49 = PkgGestorqFactory.create().newN49SetParam();
            setN49.Idq = getN49.Idq;
            setN49.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n49Set(gsuce, setN49);
        }

        protected void N57(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N57.get
            PkgGestorq.N57GetParam getN57 = PkgGestorqFactory.create().n57Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceAsignarEvaluadorParam paramsVuce = PkgGestorServicioFactory.create().newSuceAsignarEvaluadorParam();
            paramsVuce.Docnumero = getN57.Docnumero;
            paramsVuce.Usuario = getN57.Usuario;
            PkgGestorServicioFactory.create().suceAsignarEvaluador(vuce, paramsVuce);

            //N57.set
            PkgGestorq.N57SetParam setN57 = PkgGestorqFactory.create().newN57SetParam();
            setN57.Idq = getN57.Idq;
            setN57.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n57Set(gsuce, setN57);
        }

        protected void N66(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //N66.get
            PkgGestorq.N66GetParam getN66 = PkgGestorqFactory.create().n66Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.SuceRequerimientopagoParam paramsVuce = PkgGestorServicioFactory.create().newSuceRequerimientopagoParam();
            paramsVuce.Numsuce = getN66.Numesuce;
            PkgGestorServicioFactory.create().suceRequerimientopago(vuce, paramsVuce);

            // Construir el EBXML y ZIP de adjuntos
            GestorToVuce ebxml = new GestorToVuce();
            ebxml.N66(getN66.Numesuce, paramsVuce.Idnotificacion, getN66.Tasa, getN66.Motivo);


            //N66.set
            PkgGestorq.N66SetParam setN66 = PkgGestorqFactory.create().newN66SetParam();
            setN66.Idq = getN66.Idq;
            setN66.Idnotificacion = Utils.parseInteger(paramsVuce.Idnotificacion);
            PkgGestorqFactory.create().n66Set(gsuce, setN66);
        }

        // Reenviar Notificación
        protected void SERV1(JSONObject jo, Connection gsuce, Connection vuce)
        {
            //SERV1.get
            PkgGestorq.Serv1GetParam getServ1 = PkgGestorqFactory.create().serv1Get(gsuce, jo);

            // Llamar a VUCE
            PkgGestorServicio.ReenviarNotificacionParam param = PkgGestorServicioFactory.create().newReenviarNotificacionParam();
            param.Idnotificacion = getServ1.Idnotificacion;
            PkgGestorServicioFactory.create().reenviarNotificacion(vuce, param);

            //SERV1.set
            PkgGestorq.Serv1SetParam setServ1 = PkgGestorqFactory.create().serv1Set(gsuce, jo);
        }

    }
}
